
import { Component, OnInit, ViewChild } from '@angular/core';
import 'rxjs/add/operator/map';
import { CommService } from '../../comm/comm.service';
import { ReportUrls } from '../../comm/report.urls.conf';
import { SubArea } from '../../model/report/sub-area';
import { QueryModel } from '../../model/report/query-model';
import 'rxjs/add/operator/toPromise';
declare const $: any;
import { TVResource } from 'app/ptpmc/model/comm/tv-resource';
import { DxDropDownBoxComponent } from 'devextreme-angular';
import { TMSc } from 'app/ptpmc/model/report/tmsc';
@Component({
    selector: 'app-config-sc',
    templateUrl: './config-sc.component.html',
    styleUrls: ['./config-sc.component.css']
})

export class ConfigSCComponent implements OnInit {
    title = '全业务链配置';
    //弹出框
    private dialogGrid: TMSc = new TMSc();
    /*生产线 - 班次*/
    public subAreaItems: SubArea[];
    public shiftItems: any[];
    public equipemntItems: TVResource[] = [];
    public subAareSelected: any;
    public shiftSelected: any;
    public equipmentSelected: TVResource[];
    public userList: any[];
    public items: any[];
    public timeItems: any[];
    public userItems: any[];
    public userSelected: any[];
    public demandSelected: any[];
    public subAreaItems1: SubArea[];
    public subAareSelected1: any;
    public timeRange = '';
    public searchUserStr = '';
    public modalTitle = '';
    public modalType = 1;
    sxTarget = 0;
    xxTarget = 0;
    public tmscListUrl: any[];
    public listCharts: any[];

    @ViewChild('dddb') dddb: DxDropDownBoxComponent;
    @ViewChild('dddf') dddf: DxDropDownBoxComponent;

    constructor(private commServices: CommService) {
    }

    ngOnInit() {
        this.getAllUser();
        this.shiftItems = this.commServices.shifts;
        this.shiftSelected = this.shiftItems[0];
        this.subAreaItems = this.commServices.getSubAreas();
        this.subAareSelected = this.subAreaItems[0];
        this.equipemntItems = this.commServices.getTVResourceBySubArea(this.subAreaItems[0].subArea);
        this.equipmentSelected = this.equipemntItems;
        this.userSelected = new Array();
        this.demandSelected = new Array();
        this.subAreaItems1 = this.commServices.getSubAreas();
        this.subAareSelected1 = this.subAreaItems1[0];

        this.commServices.initDatePicker2($('#dateTimeRange'), 'YYYY-MM-DD', true);
        this.getReportData();
    }

    clearEquipmentSelected() {
        this.userSelected = [];
    }
    equipmentSelectedOk() {
        this.dddb.instance.close();
    }
    cleardemandSelected() {
        this.demandSelected = [];
    }
    demandSelectedOk() {
        this.dddf.instance.close();
    }

    onSubAreaSelectedChange(subArea: any): void {
        this.equipemntItems = this.commServices.getTVResourceBySubArea(subArea.value.subArea);
        this.equipmentSelected = this.equipemntItems;
    }



    getAllUser() {
        debugger
        this.commServices.getList({}, ReportUrls.AllUser).subscribe(modle => {
            if (modle.resultCode === 200) {
                modle.data.allUser = modle.data.allUser.filter(e=>e.name !=='');
                this.userList = modle.data.allUser;
                this.userItems = modle.data.allUser;
            } else {
                console.log(modle.resultCode + ',' + modle.resultMessage);
            }
        });
    }


    /**
     * 获取表格数据
     */
    getReportData(): void {

        const queryModel: QueryModel = new QueryModel();
        //queryModel.dateTimeRange = this.getTime('DateTimeRange');
        queryModel.querySubAreaIds = [this.subAareSelected.subArea];
        const url: string = ReportUrls.TMScListUrl;

        this.commServices
            .getList(queryModel, url)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.tmscListUrl = model.data;
                    this.listCharts = this.tmscListUrl;
                } else {
                    console.log(model.resultCode + ',' + model.resultMessage);
                }
            });
    }


    addData() {
        const addModel: TMSc = new TMSc();
        addModel.subArea = this.subAareSelected.subArea;
        addModel.doTimeP = this.getTime('dateTimeRange');
        addModel.responsePersonA = this.userSelected.map(e => e.email).join(',');
        addModel.demandPerson = this.demandSelected.map(e => e.email).join(',');
        const url: string = ReportUrls.TMScAddUrl;
        this.commServices
            .doPost(url, addModel)
            .subscribe(model => {
                this.items = [];
                if (model.resultCode === 200) {
                    this.items = model.data;
                    this.getReportData();
                } else {
                    console.log(model.resultCode + ',' + model.resultMessage);
                }
            });
        $("#InsertModal").modal('toggle');
    }
    openInsertDialog(itemData: TMSc): void {
        $('#InsertModal').modal('toggle');
    }
    getTime(id: any): String {
        return $('#' + id).val();
    }

    searchUser() {
        if (this.searchUserStr == '') {
            this.userList = this.userItems;
        } else {
            this.userList = this.userList.filter(e => e.loginName.indexOf(this.searchUserStr) !== -1);
        }

    }

    addEmailUser(user) {
        if (this.modalType == 1) {
            if (this.userSelected.find(e => e.email == user.email) !== undefined) {
                this.userSelected = this.userSelected.filter(e => e.email !== user.email)
            } else {
                this.userSelected.push(user);
            }
        } else {
            if (this.demandSelected.find(e => e.email == user.email) !== undefined) {
                this.demandSelected = this.demandSelected.filter(e => e.email !== user.email)
            } else {
                this.demandSelected.push(user);
            }
        }
    }

    userIsSelected(user) {
        if (this.modalType == 1) {
            return this.userSelected.find(e => e.email == user.email) !== undefined;
        } else {
            return this.demandSelected.find(e => e.email == user.email) !== undefined;
        }
    }


    addUser(type) {
        this.modalType = type;
        $('#userModal').modal('toggle');
        if (type == 1) {
            this.modalTitle = '选择责任人';
        } else {
            this.modalTitle = '选择需求人';
        }
    }

    userStr(type): string {
        return type === 1 ? this.userSelected.map(e => e.name).join(', ') : this.demandSelected.map(e => e.name).join(', ');
    }

}
